/*
 * @Author: Du.M
 * @Date: 2021-06-30 10:54:27
 * @LastEditTime: 2021-07-06 16:24:58
 * @FilePath: \express_basics\utils\index.js
 */
const mysql = require('mysql');
const {
    mysqlConfig 
} = require('../config/config');

// 连接mysql
const connect = () => {
    const {
        host,
        user,
        password,
        database
    } = mysqlConfig;
    return mysql.createConnection({
        host,
        user,
        password,
        database
    })
}

// 新建查询连接
const querySql = (sql) => {
    const connection = connect();
    return new Promise((resolve, reject) => {
        try {
            connection.query(sql, (err, res) => {
            if (err) {
              reject(err);
            } else {
              resolve(res);
            }
          })
        } catch (e) {
          reject(e);
        } finally {
          //释放连接
          connection.end();
        }
      })
}

module.exports = {
    querySql
}